package cn.itsource.system.service.impl;

import cn.itsource.system.domain.Menu;
import cn.itsource.system.mapper.MenuMapper;
import cn.itsource.system.query.MenuQuery;
import cn.itsource.system.service.MenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
@CacheConfig(cacheNames = "cache_Menu_type")
public class MenuServiceImpl implements MenuService {
    
    @Autowired
    private MenuMapper menuMapper;


    /**
     * 增
     * @param m
     */
    @Override
    @Transactional
    @CacheEvict(key = "'cache_Menu_type'")
    public void insert(Menu m) {
        menuMapper.insert(m);
    }

    @Transactional
    @Override
    @CacheEvict(key = "'cache_Menu_type'")
    public void updateByPrimaryKeySelective(Menu m) {
        menuMapper.updateByPrimaryKeySelective(m);
    }



    @Override
    public PageInfo<Menu> pageInfo(MenuQuery menuQuery) {
        PageHelper.startPage(menuQuery.getCurrentPage(),menuQuery.getPageSize());
        List<Menu> permissions = menuMapper.selectAll(menuQuery);
        return new PageInfo<Menu>(permissions);
    }


    @Override
    @Transactional
    @CacheEvict(key = "'cache_Menu_type'")
    public void deleteAll(List<Menu> m) {
        menuMapper.deleteAll(m);
    }

    @Override
    @Transactional
    @CacheEvict(key = "'cache_Menu_type'")
    public void deleteByPrimaryKey(Long id) {
        menuMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Cacheable(key = "'cache_Menu_type'")
    public List<Menu> gettree() {
        //查询所有文章类型
        List<Menu> all = menuMapper.selectAllType();

        ArrayList<Menu> tree = new ArrayList<>();

        //拉姆达表达式
        /*Map<Long, Menu> collect = all.stream()
                .collect(Collectors.toMap(Menu::getId, Function.identity()));*/
        Map<Long, Menu> collect = all.stream().collect(Collectors.toMap(new Function<Menu, Long>() {
            @Override
            public Long apply(Menu menu) {
                return menu.getId();
            }
        }, new Function<Menu, Menu>() {
            @Override
            public Menu apply(Menu menu) {
                return menu;
            }
        }));
        //循环文章类型
        for (Menu menu : all) {
            //如果父级为空，就是一级
            if (menu.getParentId() == null) {
                tree.add(menu);
            } else {
                //拿到父级类型对象
                Menu menu1 = collect.get(menu.getParentId());
                //装进去
                menu1.getChildren().add(menu);
            }
        }
        //放入集合
        return tree;

    }
}
